<%-- 
    Document   : configurationMgmt
    Created on : Dec 15, 2011, 6:02:37 PM
    Author     : Randy
--%>

<%@page import="java.util.HashMap"%>
<%@page import="model.SystemUser"%>
<%@page import="model.Configuration"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Configuration Management</title>
        <%@include file = "global/admin_include.jsp" %>
        <%
            HashMap am = new HashMap();
            Object obj = request.getSession().getAttribute("accessMap");
            if (obj == null) {
                response.sendRedirect("../admin.jsp");
            } else {
                am = (HashMap) obj;
                if (am.get("G").equals("false")) {
                    response.sendRedirect("accessDenied.jsp");
                }
            }
        %>
        <script type="text/javascript">
            $(document).ready(function(){
                $('#tt').datagrid({
                    onBeforeEdit:function(index,row){  
                        row.editing = true;  
                        updateActions();  
                    },  
                    onAfterEdit:function(index,row){
                        row.editing = false;
                        $.ajax({
                            type: "POST",
                            url: "../ConfigurationServlet",
                            data: "action=updateConfiguration"
                                + "&config_id=" + row.config_id
                                + "&status=" + row.status,
                            dataType: "json",
                            cache: false,
                            success: function(data){
                                if((data.type!='error')){
                                    $('#tt').datagrid('reload');
                                }
                                alertMsg(data);
                            }
                        });
                        updateActions();
                    },  
                    onCancelEdit:function(index,row){
                        row.editing = false;  
                        updateActions(); 
                    }
                });
            });
            
            function formatActions(value,row,index){ 
                if (row.editing) {  
                    var s = '<input type="button" onclick="saverow('+index+')" value="Save"/> ';  
                    var c = '<input type="button" onclick="cancelrow('+index+')" value="Cancel"/>';  
                    return s+c;  
                } else {  
                    var e = '<input type="button" onclick="editrow('+index+')" value="Edit"/> ';
                    return e;  
                }
            }
            
            function editrow(index){  
                $('#tt').datagrid('beginEdit', index);
            }  
             
            function saverow(index){  
                $('#tt').datagrid('endEdit', index);  
            }
            
            function cancelrow(index){  
                $('#tt').datagrid('cancelEdit', index);  
            }
            
            function updateActions(){  
                var rowcount = $('#tt').datagrid('getRows').length;  
                for(var i=0; i<rowcount; i++){ 
                    $('#tt').datagrid('updateRow',{index:i,row:{actions:''}});  
                }  
            }
        </script>
    </head>
    <body>
        <div style="border-bottom:1px solid #ccc"><h2>Configuration Management</h2></div> 
        <br/>
        <div style="height:450px">
            <table id="tt" class="easyui-datagrid" fit="true"
                   url="../ConfigurationServlet?action=getConfigurations"
                   singleSelect="true" fitColumns="true" pagination="false"
                   rownumbers="false">
                <thead>  
                    <tr>
                        <th field="config_id" width="5%">ID</th>
                        <th field="name" width="75%">Name</th>
                        <th field="status" editor="{
                            type:'checkbox',
                            options:{on: '<%=Configuration.ConfigurationStatus.True.name()%>',off: '<%=Configuration.ConfigurationStatus.False.name()%>'}
                            }" width="5%" align="center">Status</th>
                        <th field="actions" width="15%" align="center" formatter="formatActions"></th>
                    </tr>  
                </thead>  
            </table> 
        </div>
    </body>
</html>

